package cn.edu.xhu.library_management.area.controller;

import cn.edu.xhu.library_management.area.entity.LmFloor;
import cn.edu.xhu.library_management.area.service.FloorService;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 13
 * @Description: 楼层控制器
 */
@Api(tags = "楼层模块")
@ApiSort(2)
@RestController
@RequestMapping("/lm_area")
public class FloorController {

    @Autowired
    private FloorService floorService;

    @GetMapping("/floor")
    @ApiOperation(value = "通过图书馆ID查询所有楼层信息")
    @ApiOperationSupport(order = 211)
    public R findFloorsByInfoId(@Param("infoId") Integer infoId){
        List<LmFloor> floors = floorService.findFloorsByInfoId(infoId);
        floors.sort((o1, o2) -> o1.getIndex() - o2.getIndex());
        return !floors.isEmpty()?R.ok().data("floors",floors): R.error("数据为空");
    }
    @GetMapping("/floor/type")
    @ApiOperation(value = "通过图书馆ID和特定类型查询楼层信息")
    @ApiOperationSupport(order = 211)
    public R findFloorsByType(@Param("infoId") Integer infoId,@RequestParam("type") Integer type){
        List<LmFloor> floors = floorService.findFloorsByType(infoId,type);
        floors.sort((o1, o2) -> o1.getIndex() - o2.getIndex());
        return !floors.isEmpty()?R.ok().data("floors",floors): R.error("数据为空");
    }
    @GetMapping("/floor/{id}")
    @ApiOperation(value = "通过楼层ID查询楼层信息")
    @ApiOperationSupport(order = 212)
    public R findFloorById(@PathVariable("id") Integer id){
        LmFloor floor = floorService.findFloorById(id);
        return floor != null ? R.ok().data("floor",floor):R.error("查无此数据!!!");
    }

    @PostMapping("/floor")
    @ApiOperation(value = "添加楼层")
    @ApiOperationSupport(order = 221)
    public R addFloor(@RequestBody LmFloor floor){
        return floorService.addFloor(floor) ? R.ok("添加楼层信息成功!!!"):R.error("添加楼层信息失败!!!");
    }

    @PutMapping("/floor")
    @ApiOperation(value = "修改楼层")
    @ApiOperationSupport(order = 231)
    public R updateFloor(@RequestBody LmFloor floor){

        return floorService.updateFloor(floor) ? R.ok("修改楼层信息成功!!!"):R.error("修改楼层信息失败!!!");
    }

    @DeleteMapping("/floor/{id}")
    @ApiOperation(value = "删除楼层")
    @ApiOperationSupport(order = 241)
    public R deleteFloorById(@PathVariable("id") Integer id){
        return floorService.deleteFloorById(id) ? R.ok("删除楼层信息成功!!!"):R.error("删除楼层信息失败!!!");
    }
}
